/**
 *
 * @Title: MdlbActivity.java
 * @Package com.idrem.views.bf
 * @Description: TODO(用一句话描述该文件做什么)
 * @author WangZhengCai
 * @date 2012 2012-11-13 下午8:07:59
 * @version V1.0
 *
 */
package com.idrem.views.bf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.idrem.R;

/**
 * 
 * @ClassName: MdlbActivity
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author WangZhengCai
 * @date 2012 2012-11-13 下午8:07:59
 * 
 * 
 */
public class MdlbActivity extends CommonActivity implements OnItemClickListener {

	private static String TAG = "MdlbActivity";
	private static final int PHOTO_PICKED_WITH_DATA = 3021;
	private static final int CAMERA_WITH_DATA = 3023;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mdlb);

		super.listViewAll = (ListView) findViewById(android.R.id.list);
		setListAdapter(new SimpleAdapter(MdlbActivity.this, getDate(),
				R.layout.md_listview, new String[] { "img", "text_md_cd",
						"text_md_nm", "text_md_type", "text_bf_flag",
						"text_bf_distance", "img_pre" },
				new int[] { R.id.img, R.id.text_md_cd, R.id.text_md_nm,
						R.id.text_md_type, R.id.text_bf_flag,
						R.id.text_bf_distance, R.id.img_pre }));
		super.listViewAll.setOnItemClickListener(this);
		// // 进入该界面时,模仿从服务器加载数据时的虚拟进度条
		// super.progressDialog = ProgressDialog.show(this, "iDREM",
		// "数据获取中....",
		// true);
		// super.progressDialog.show();
		// // 通过线程来循环调用进度条
		// super.handler.post(this);
	}

	private List<Map<String, Object>> getDate() {
		List<Map<String, Object>> listBoy = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < mdNm.length; i++) {
			Map<String, Object> mapBoy = new HashMap<String, Object>();
			mapBoy.put("text_md_cd", mdCd[i]);
			mapBoy.put("text_md_nm", mdNm[i]);
			mapBoy.put("text_md_type", mdType[i]);
			mapBoy.put("text_bf_flag", mdBfFlsg[i]);
			mapBoy.put("text_bf_distance", mdDistance[i]);
			mapBoy.put("img", R.drawable.toright_mark);
			if (i % 2 == 0) {
				mapBoy.put("img_pre", R.drawable.png_none);
			} else {
				mapBoy.put("img_pre", R.drawable.png_demo);
			}
			listBoy.add(mapBoy);
		}
		return listBoy;
	}

	// 门店代码
	static final String[] mdCd = { "104001", "300002", "301005", "400012",
			"302043", "400001", "800412", "625100", "406058" };
	// 门店名称
	static final String[] mdNm = { "百盛超市浪高点", "十分力总部", "联佳超市大堰二村", "联佳超市马王乡店",
			"家佳超市马王乡店", "心连心马王乡", "好又多超市学府店", "太阳系超市高新园区店", "家乐福超市福家店" };
	// 门店类型
	static final String[] mdType = { "大型超市", "小型超市", "小型超市", "小型超市", "中型超市",
			"杂货店/售卖亭", "大型超市", "小型超市", "大型超市" };
	// 门店拜访标记
	static final String[] mdBfFlsg = { "未拜访", "已拜访", "未拜访", "已拜访", "未拜访",
			"已拜访", "未拜访", "已拜访", "未拜访" };
	// 门店距离
	static final String[] mdDistance = { "5.2公里", "5.6公里", "无坐标", "无坐标",
			"3.4公里", "3.6公里", "5.7公里", "7.6公里", "12.1公里" };

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		ImageView img_pre = (ImageView) arg1.findViewById(R.id.img_pre);
		img_pre.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// take a photo for md
				Toast.makeText(getApplicationContext(),
						"img_pre clicked,take a photo", Toast.LENGTH_SHORT)
						.show();
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(intent, CAMERA_WITH_DATA);

			}
		});
		intent.setClass(this, SignInActivity.class);
		startActivity(intent);

		// ImageView img_r = (ImageView) arg1.findViewById(R.id.img);
		// img_r.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // take a photo for md
		// Toast.makeText(getApplicationContext(),
		// "img_r clicked,start books", Toast.LENGTH_SHORT).show();
		// // intent.setClass(this, SignInActivity.class);
		// // startActivity(intent);
		//
		// }
		// });

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			String sdStatus = Environment.getExternalStorageState();
			if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
				Log.i("TestFile",
						"SD card is not avaiable/writeable right now.");
				return;
			}
			String name = new DateFormat().format("yyyyMMdd_hhmmss",
					Calendar.getInstance(Locale.CHINA))
					+ ".jpg";
			// Toast.makeText(this, name, Toast.LENGTH_LONG).show();
			Bundle bundle = data.getExtras();
			Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据，并转换为Bitmap图片格式

			FileOutputStream b = null;
			// 保存
			File file = new File("/sdcard/myImage/");
			file.mkdirs();// 创建文件夹
			String fileName = "/sdcard/myImage/" + name;
//			AppLog.e(TAG, "fileName is " + fileName);

			try {
				b = new FileOutputStream(fileName);
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
//				AppLog.e(TAG, "fileName saved " + fileName);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} finally {
				try {
					b.flush();
					b.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			((ImageView) findViewById(R.id.img_pre)).setImageBitmap(bitmap);// 将图片显示在ImageView里
			// Bitmap bitmapRe = this.getPic(fileName + ".jpg");
			// ((ImageView)
			// findViewById(R.id.iv_btn)).setImageBitmap(bitmapRe);//
			// 将图片显示在ImageView里
		}
	}

	public Bitmap getPic(String fileName) {
		// FileInputStream fis = new FileInputStream(fileName);
		// Bitmap bitmap = BitmapFactory.decodeStream(fis);
		Bitmap bitmap = BitmapFactory.decodeStream(getClass()
				.getResourceAsStream(fileName));
		return bitmap;
	}

	protected void doCropPhoto(Bitmap data) {
		Intent intent = getCropImageIntent(data);
		startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
	}

	public static Intent getCropImageIntent(Bitmap data) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setType("image/*");
		intent.putExtra("data", data);
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", 1);
		intent.putExtra("outputY", 1);
		intent.putExtra("return-data", true);
		return intent;
	}
}